home *** CD-ROM | disk | FTP | other *** search
/ IRIS Performer 2.2 Friends Demo / SGI IRIS Performer 2.2 Friends Demo.iso / friends / openworlds / tix / Primitiv.tcl < prev    next >
Text File  |  1997-11-22  |  9KB  |  392 lines

  1. # This is the primitive widget. It is just a frame with proper inheritance
  2. # wrapping. All new Tix widgets will be derived from this widget
  3.  
  4. # No superclass, so the -superclass switch is not used
  5. #
  6. #
  7. tixWidgetClass tixPrimitive {
  8.     -superclass {}
  9.     -classname  TixPrimitive
  10.     -method {
  11.     cget configure subwidget subwidgets
  12.     }
  13.     -flag {
  14.     -background -borderwidth -cursor
  15.     -height -highlightbackground -highlightcolor -highlightthickness
  16.     -options -relief -takefocus -width -bd -bg
  17.     }
  18.     -static {
  19.     -options
  20.     }
  21.     -configspec {
  22.     {-background background Background #d9d9d9} 
  23.     {-borderwidth borderWidth BorderWidth 0} 
  24.     {-cursor cursor Cursor {}} 
  25.     {-height height Height 0}
  26.     {-highlightbackground highlightBackground HighlightBackground #c3c3c3}
  27.     {-highlightcolor highlightColor HighlightColor black}
  28.     {-highlightthickness highlightThickness HighlightThickness 0} 
  29.     {-options options Options {}}
  30.     {-relief relief Relief flat}
  31.     {-takefocus takeFocus TakeFocus 0} 
  32.     {-width width Width 0}
  33.     }
  34.     -alias {
  35.     {-bd -borderwidth}
  36.     {-bg -background}
  37.     }
  38. }
  39.  
  40. #----------------------------------------------------------------------
  41. # ClassInitialization:
  42. #----------------------------------------------------------------------
  43.  
  44. # Override: never
  45. proc tixPrimitive::Constructor {w args} {
  46.  
  47.     upvar #0 $w data
  48.     upvar #0 $data(className) classRec
  49.  
  50.     # Set up some minimal items in the class record.
  51.     #
  52.     set data(w:root)  $w
  53.     set data(rootCmd) $w:root
  54.  
  55.     # We need to create the root widget in order to parse the options
  56.     # database
  57.     tixCallMethod $w CreateRootWidget
  58.  
  59.     # Parse the default options from the options database
  60.     #
  61.     tixPrimitive::ParseDefaultOptions $w
  62.  
  63.     # Parse the options supplied by the user
  64.     #
  65.     tixPrimitive::ParseUserOptions $w $args
  66.  
  67.     # Rename the widget command so that it can be use to access
  68.     # the methods of this class
  69.  
  70.     tixPrimitive::MkWidgetCmd $w
  71.  
  72.     # Inistalize the Widget Record
  73.     #
  74.     tixCallMethod $w InitWidgetRec
  75.  
  76.     # Construct the compound widget
  77.     #
  78.     tixCallMethod $w ConstructWidget
  79.  
  80.     # Do the bindings
  81.     #
  82.     tixCallMethod $w SetBindings
  83.  
  84.     # Call the configuration methods for all "force call" options
  85.     #
  86.     foreach option $classRec(forceCall) {
  87.     tixInt_ChangeOptions $w $option $data($option)
  88.     }
  89. }
  90.  
  91.  
  92. # Create only the root widget. We need the root widget to query the option
  93. # database.
  94. #
  95. # Override: seldom. (unless you want to use a toplevel as root widget)
  96. # Chain   : never.
  97.  
  98. proc tixPrimitive::CreateRootWidget {w args} {
  99.     upvar #0 $w data
  100.     upvar #0 $data(className) classRec
  101.  
  102.     frame $w -class $data(ClassName)
  103. }
  104.  
  105. proc tixPrimitive::ParseDefaultOptions {w} {
  106.     upvar #0 $w data
  107.     upvar #0 $data(className) classRec
  108.  
  109.     # SET UP THE INSTANCE RECORD ACCORDING TO DEFAULT VALUES IN
  110.     # THE OPTIONS DATABASE
  111.     #
  112.     foreach option $classRec(options) {
  113.     set spec [tixInt_GetOptionSpec $data(className) $option]
  114.  
  115.     if {[lindex $spec 0] == "="} {
  116.         continue
  117.     }
  118.  
  119.     set o_name    [lindex $spec 1]
  120.     set o_class   [lindex $spec 2]
  121.     set o_default [lindex $spec 3]
  122.  
  123.     if {![catch "option get $w $o_name $o_class" db_default]} {
  124.         if {$db_default != ""} {
  125.         set data($option) $db_default
  126.         } else {
  127.         set data($option) $o_default
  128.         }
  129.     } else {
  130.         set data($option) $o_default
  131.     }
  132.     }
  133. }
  134.  
  135. proc tixPrimitive::ParseUserOptions {w arglist} {
  136.     upvar #0 $w data
  137.     upvar #0 $data(className) classRec
  138.  
  139.     # SET UP THE INSTANCE RECORD ACCORDING TO COMMAND ARGUMENTS FROM
  140.     # THE USER OF THE TIX LIBRARY (i.e. Application programmer:)
  141.     #
  142.     tixForEach {option arg} $arglist {
  143.     if {[lsearch $classRec(options) $option] != "-1"} {
  144.         set spec [tixInt_GetOptionSpec $data(className) $option]
  145.  
  146.         if {[lindex $spec 0] != "="} {
  147.         set data($option) $arg
  148.         } else {
  149.         set realOption [lindex $spec 1]
  150.         set data($realOption) $arg
  151.         }
  152.     } else {
  153.         error "unknown option $option. Should be: [tixInt_ListOptions $w]"
  154.     }
  155.     }
  156. }
  157.  
  158. #----------------------------------------------------------------------
  159. # Initialize the widget record
  160. #
  161. # Override: always
  162. # Chain   : always, before
  163. proc tixPrimitive::InitWidgetRec {w} {
  164.     # default: do nothing
  165. }
  166.  
  167. #----------------------------------------------------------------------
  168. # SetBindings
  169. #
  170. # Override: sometimes
  171. # Chain   : sometimes, before
  172. #
  173. bind Foo <Destroy> {
  174.     [tixGetMethod %W [set %W(className)] Destructor] %W
  175. }
  176.  
  177. proc tixPrimitive::SetBindings {w} {
  178.     upvar #0 $w data
  179.  
  180.     if {[winfo toplevel $w] == $w} {
  181.     bindtags $w [concat Foo [bindtags $w]]
  182.     } else {
  183.     bind $data(w:root) <Destroy> \
  184.         "[tixGetMethod $w $data(className) Destructor] $w"
  185.     }
  186. }
  187.  
  188. #----------------------------------------------------------------------
  189. # PrivateMethod: ConstructWidget
  190. # Construct and set up the compound widget
  191. #
  192. # Override: sometimes
  193. # Chain   : sometimes, before
  194. #
  195. proc tixPrimitive::ConstructWidget {w} {
  196.     upvar #0 $w data
  197.  
  198.     $data(rootCmd) config \
  199.     -background  $data(-background) \
  200.     -borderwidth $data(-borderwidth) \
  201.     -cursor      $data(-cursor) \
  202.     -relief      $data(-relief)
  203.  
  204.     if {$data(-width) != 0} {
  205.     $data(rootCmd) config -width $data(-width)
  206.     }
  207.     if {$data(-height) != 0} {
  208.     $data(rootCmd) config -height $data(-height)
  209.     }
  210.  
  211.     set rootname *[string range $w 1 end]
  212.  
  213.     tixForEach {spec value} $data(-options) {
  214.     option add $rootname*$spec $value 100
  215.     }
  216. }
  217.  
  218. #----------------------------------------------------------------------
  219. # PrivateMethod: MkWidgetCmd
  220. # Construct and set up the compound widget
  221. #
  222. # Override: sometimes
  223. # Chain   : sometimes, before
  224. #
  225. proc tixPrimitive::MkWidgetCmd {w} {
  226.     upvar #0 $w data
  227.  
  228.     rename $w $data(rootCmd)
  229.     tixInt_MkInstanceCmd $w
  230. }
  231.  
  232.  
  233. #----------------------------------------------------------------------
  234. # ConfigOptions:
  235. #----------------------------------------------------------------------
  236.  
  237. #----------------------------------------------------------------------
  238. # ConfigMethod: config
  239. #
  240. # Configure one option.
  241. # Override: always
  242. # Chain   : automatic.
  243. #
  244. proc tixPrimitive::config {w option value} {
  245.     upvar #0 $w data
  246.  
  247.     case $option {
  248.     {-background -borderwidth -cursor -height -relief
  249.      -width -bd -bg} {
  250.          $data(rootCmd) config $option $value
  251.      }
  252.     }
  253. }
  254.  
  255. #----------------------------------------------------------------------
  256. # PublicMethods:
  257. #----------------------------------------------------------------------
  258.  
  259. #----------------------------------------------------------------------
  260. # This method is used to implement the "subwidgets" widget command.
  261. # Will be re-written in C. It can't be used as a public method because
  262. # of the lame substring comparison routines used in tixClass.c
  263. #
  264. #
  265. proc tixPrimitive::subwidgets {w type args} {
  266.     upvar #0 $w data
  267.  
  268.     case $type {
  269.     -class {
  270.         set name [lindex $args 0]
  271.         set args [lrange $args 1 end]
  272.         # access subwidgets of a particular class
  273.         #
  274.         # note: if $name=="Frame", will *not return the root widget as well
  275.         #
  276.         set sub {}
  277.         foreach des [tixDescendants $w] {
  278.         if {[winfo class $des] == $name} {
  279.             lappend sub $des
  280.         }
  281.         }
  282.  
  283.         # Note: if the there is no subwidget of this class, does not
  284.         # cause any error.
  285.         #
  286.         if {$args == {}} {
  287.         return $sub
  288.         } else {
  289.         foreach des $sub {
  290.             eval $des $args
  291.         }
  292.         return ""
  293.         }
  294.     }
  295.     -group {
  296.         set name [lindex $args 0]
  297.         set args [lrange $args 1 end]
  298.         # access subwidgets of a particular group
  299.         #
  300.         if [info exists data(g:$name)] {
  301.         if {$args == {}} {
  302.             set ret {}
  303.             foreach item $data(g:$name) {
  304.             lappend ret $w.$item
  305.             }
  306.             return $ret
  307.         } else {
  308.             foreach item $data(g:$name) {
  309.             eval $w.$item $args
  310.             }
  311.             return ""
  312.         }
  313.         } else {
  314.         error "no such subwidget group $name"
  315.         }
  316.     }
  317.     -all {
  318.         set sub [tixDescendants $w]
  319.  
  320.         if {$args == {}} {
  321.         return $sub
  322.         } else {
  323.         foreach des $sub {
  324.             eval $des $args
  325.         }
  326.         return ""
  327.         }
  328.     }
  329.     default {
  330.         error "unknown flag $type, should be -all, -class or -group"
  331.     }
  332.     }
  333. }
  334.  
  335. #----------------------------------------------------------------------
  336. # PublicMethod: subwidget
  337. #
  338. # Access a subwidget withe a particular name 
  339. #
  340. # Override: never
  341. # Chain   : never
  342. #
  343. proc tixPrimitive::subwidget {w name args} {
  344.     upvar #0 $w data
  345.  
  346.     if [info exists data(w:$name)] {
  347.     if {$args == {}} {
  348.         return $data(w:$name)
  349.     } else {
  350.         return [eval $data(w:$name) $args]
  351.     }
  352.     } else {
  353.     error "no such subwidget $name"
  354.     }
  355. }
  356.  
  357.  
  358. #----------------------------------------------------------------------
  359. # PrivateMethods:
  360. #----------------------------------------------------------------------
  361.  
  362. # delete the widget record and remove the command
  363. #
  364. proc tixPrimitive::Destructor {w} {
  365.     upvar #0 $w data
  366.  
  367.     if {![info exists data(w:root)]} {
  368.     return
  369.     }
  370.  
  371.     if {[info commands $w] != ""} {
  372.     # remove the command
  373.     #
  374.     rename $w {}
  375.     }
  376.  
  377.     if {[info commands $data(rootCmd)] != ""} {
  378.     # remove the command of the root widget
  379.     #
  380.     rename $data(rootCmd) {}
  381.     }
  382.  
  383.     # delete the widget record
  384.     #
  385.     catch {unset data}
  386. }
  387.